//
//  YHGROtherdown.m
//  test
//
//  Created by 李彦宏 on 2018/3/10.
//  Copyright © 2018年 朱圳. All rights reserved.
//已同意

#import "YHGROtherdown.h"
#import "UnConfHead.h"//section==0头部
#import "UnContHead.h"//section==1头部
#import "FeeDetailCell.h"//内容详情cell
#import "FeeMangerModel.h"//费用model
#import "Utility.h"

#define FeeDetailCellID @"FeeDetailCell1"

@interface YHGROtherdown ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectArry;//开合Arry
@property (nonatomic, strong) FeeMangerModel *model;
@property (nonatomic, strong) NSMutableArray *contentArry;
@end

@implementation YHGROtherdown
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:tableView];
    self.tableView=tableView;
    [tableView registerNib:[UINib nibWithNibName:@"FeeDetailCell" bundle:nil] forCellReuseIdentifier:FeeDetailCellID];
    
}

#pragma -mark ——————————已同意的数据———————————————————
-(void)setFeedata{
    NSDictionary *dic=@{@"gid":Gid,
                        @"gpass":Gpassword,
                        @"state":@"1",
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl:GR_OtherFeeSe parmas:dic successData:^(id json) {
        [self.contentArry removeAllObjects];
        __weak typeof(self) weakslf = self;
        NSArray *arry=json[@"otherlist"];
        for (int index=0; index<arry.count; index++) {
            weakslf.model=[FeeMangerModel yy_modelWithJSON:arry[index]];
            [weakslf.contentArry addObject:weakslf.model];
        }
        for(int i=0;i<arry.count+1;i++){
            [weakslf.selectArry addObject:@"0"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if(arry.count==0){
                [self.tableView removeFromSuperview];
                KongDataView *kview=[KongDataView NilDataview];
                kview.frame=self.view.frame;
                [self.view addSubview:kview];
            }
            
            [weakslf.tableView reloadData];
        });
        
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setFeedata];
        [EasyShowLodingView hidenLoding];
    });
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
     [self setupTable];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // Do any additional setup after loading the view.
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.selectArry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.selectArry[section] isEqualToString:@"1"]) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FeeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:FeeDetailCellID];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    self.model=_contentArry[indexPath.section-1];
    cell.DetailTextView.text=self.model.discrabe;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section==0){
        UnConfHead *head1=[[UnConfHead alloc]init];
        head1.SumMoney.text=@"金额";
        return head1;
    }else{
        self.model=_contentArry[section-1];
        UnContHead *head=[[UnContHead alloc]init];
        head.IsSelet.textColor=[UIColor greenColor];
        head.IsSelet.text=@"未支付";
        head.IsSelet.textColor=XGColor;
        NSString *Time=[Utility getDateStringWithTimeStr:self.model.time];
        head.data.text=Time;
        head.sum.text=self.model.money;
        //添加手势
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAction:)];
        [head addGestureRecognizer:tap];
        head.tag=section;
        return head;
    }
    
}
//手势响应
- (void)tapImgAction:(UIGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    
    if ([_selectArry[index] isEqualToString:@"0"]) {
        
        [_selectArry replaceObjectAtIndex:index withObject:@"1"];
        
    }else{
        [_selectArry replaceObjectAtIndex:index withObject:@"0"];
        
    }
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
}


-(NSMutableArray *)selectArry{
    if (!_selectArry) {
        _selectArry = [[NSMutableArray alloc]init];
    }
    return _selectArry;
}
-(NSMutableArray *)contentArry{
    if(_contentArry==nil){
        _contentArry=[[NSMutableArray alloc]init];
    }
    return _contentArry;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
